import { queryStationOrderLimit } from '../../services/inspect-project'
import dayjs from 'dayjs'
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    showPopup:false,
    stationId:"",
    pickColumns:[
      {
        values:['上午', '下午'],
        className: 'column1',
      },
      {
        values:['剩5个名额']
      }
    ],
    selectedDate:'',
    selectedTime:'am',
    selectedPlaceNum:0,
    placesMap:{
      'am':5,
      'pm':3
    },
    placesList:[],
    formatter(day) {
      return day;
    },
  },

  onLoad(options){
    if(!options){
      return
    }
    const { selectedDate, selectedTime } = options
    console.log("selectedDate, selectedTime>>>>", selectedDate, selectedTime)
    if(selectedDate && selectedTime){
      this.setData({
        selectedDate,
        selectedTime,
        formatter:function(day){
          console.log("day>>>",day)
          const nowDate = dayjs(day.date).format('YYYY-MM-DD') 
          if(selectedDate == nowDate){
            day.bottomInfo = selectedTime == 'am' ? '上午' : '下午'
          }
          return day
        } 
      })
    }
  },

  async getStationOrderLimit(data){
    let limitRes = await queryStationOrderLimit(data)
    console.log("limitRes>>>", limitRes)
    let limitNum = 5
    if(limitRes.code === 200){
      let amItem = limitRes.rows.find(item => item.time == 'am')
      limitNum = amItem.num
    }
    this.setData({
      placesList: limitRes.rows,
      pickColumns:[
        {
          values:['上午', '下午'],
          className: 'column1',
        },
        {
          values:[`剩${limitNum}个名额`]
        }
      ]
    })
  },

  selectTime(date){
    const stationId = app.globalData.globalInfo.stationId
    const selectedDate = dayjs(date.detail).format('YYYY-MM-DD')
    this.getStationOrderLimit({
      "date": selectedDate,
      "stationId": stationId
    })

    this.setData({
      showPopup:true,
      selectedDate,
    })
  },

  onCancel(cancalRes){
    this.setData({
      showPopup:false
    })
  },

  onConfirm(confirmRes){
    console.log("confirmRes>>>", confirmRes)
    const confirmInfo = {
      selectedDate:this.data.selectedDate,
      selectedTime:this.data.selectedTime,
      selectedPlaceNum:this.data.selectedPlaceNum
    }
    app.globalData.globalInfo.appointDate = this.data.selectedDate
    app.globalData.globalInfo.appointTime = this.data.selectedTime
    this.setData({
      showPopup:false
    })
    wx.navigateBack()
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit("infoChange", confirmInfo)

  },

  onChangePicker(event){
    const { picker, value, index } = event.detail
    console.log("picker, value, index>>>", picker, value, index)
    let placeNumber = 0
    if(value[0] == '上午'){
      let amItem = this.data.placesList.find(item => item.time === 'am')
      placeNumber = amItem.num
    }else{
      let pmItem = this.data.placesList.find(item => item.time === 'pm')
      placeNumber = pmItem.num
      // placeNumber = this.data.placesMap['pm']
    }
    this.setData({
      selectedTime:value[0] == '上午' ? 'am' : 'pm',
      selectedPlaceNum: placeNumber
    })
    picker.setColumnValues(1, [`剩${placeNumber}个名额`]);
  },

})